﻿@model IEnumerable<WalletManager.Models.MovementOfFundsModel>

@{
    ViewBag.Title = "Index";
}
@{
    decimal TotalIncome = 0;
    decimal TotalExpences = 0;
}
<br />

<p>

    <a data-toggle="modal" data-target="#myModalCreate" style="cursor:pointer;" class="btn btn-primary">
        <i class="glyphicon glyphicon-plus"></i>
        Add
    </a>
    
    @*<a data-toggle="modal" data-target="#myModalReport" style="cursor:pointer;" class="btn btn-info">
        <i class="glyphicon glyphicon-download-alt"></i>
        Export
    </a>*@
</p>
@*@using (Ajax.BeginForm("Index", "MovementOfFunds", new AjaxOptions
 {
     InsertionMode = InsertionMode.InsertAfter,
     HttpMethod = "GET",
     UpdateTargetId = "dialogDiv"
 }, new { @class = "modal-form", id = "formClick", encType = "multipart/form-data" }))
{
    <table class="table">
        <tbody>
            <tr>

                <td>Начална дата: @Html.TextBox("startPeriod", null, new { @class = "startPeriod", style = "width:150px" })</td>
                @*<td>Крайна дата: @Html.TextBox("endPeriod", null, new { @class = "endPeriod", style = "width:150px" })</td>*@
             

                @*<td><button type="submit" value="filter" data-backdrop="static" data-toggle="modal" class="btnEdit btn btn-small btn-info" name="submit"><i class="glyphicon glyphicon-search"></i></button></td>

            </tr>
          
        </tbody>
    </table>
}*@


<div class="panel panel-success">
    <div class="panel-heading">Movement of funds</div>
    <div class="panel-body">
       
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>
                        @Html.DisplayNameFor(model => model.movementType.directory)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.EstimateDate)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.EstimatePrice)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.RealDate)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.RealPrice)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.sectionId)
                    </th>
                    <th></th>
                </tr>
            </thead>
            @foreach (var item in Model)
            {
                <tbody>
                    <tr>
                        <td>
                            <a id="@item.Id" data-toggle="modal" data-target="#myModal" style="cursor:pointer;">
                                @Html.DisplayFor(modelItem => item.movementType.directory)
                            </a>
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.EstimateDate)
                        </td>
                        <td>
                            @if (item.movementType.sectionId == 1)
                            {
                                <span class="redExpense">
                                    @Html.DisplayFor(modelItem => item.EstimatePrice)
                                </span>
                            }
                            else if (item.movementType.sectionId == 2)
                            {
                                <span class="greenIncome">
                                    @Html.DisplayFor(modelItem => item.EstimatePrice)
                                </span>
                            }

                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.RealDate)
                        </td>
                        <td>
                            @if (item.movementType.sectionId == 1)
                            {
                                <span class="redExpense">
                                    @Html.DisplayFor(modelItem => item.RealPrice)
                                    @{
                                        if (item.RealPrice != null)
                                        {
                                            TotalExpences += (decimal)item.RealPrice;
                                        }
                                    }
                                </span>
                            }
                            else if (item.movementType.sectionId == 2)
                            {
                                <span class="greenIncome">
                                    @Html.DisplayFor(modelItem => item.RealPrice)
                                    @{
                                        if (item.RealPrice != null)
                                        {
                                            TotalIncome += (decimal)item.RealPrice;
                                        }
                                    }
                                </span>
                            }
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.movementType.movementType.name)
                        </td>
                        <td>

                            <a id="@item.Id" data-toggle="modal" data-target="#myModal" style="cursor:pointer;">
                                <i class="fa fa-pencil-square-o fa-2x"></i>
                            </a>
                            <a id="@item.Id" data-toggle="modal" data-target="#myModalDelete" style="cursor:pointer;">
                                <i class="fa fa-recycle fa-2x"></i>
                            </a>
                            <a id="@item.Id" href="@Url.Action("PDF", "MovementOfFunds", new { id = @item.Id })" style="cursor:pointer;" target="_blank">
                                <i class="fa fa-file-pdf-o fa-2x"></i>
                            </a>
                        </td>
                    </tr>
                </tbody>
            }
            @{
                decimal Balance = TotalIncome + TotalExpences;
             }
            <tr>
                <td><h4><span class="label label-danger">Total Expenses: @TotalExpences</span></h4></td>
                <td><h4><span class="label label-success">Total Incomes: @TotalIncome</span></h4></td>
                <td><h4><span class="label label-warning">Balance: @Balance</span></h4></td>
                @if (Balance < 0)
                {
                    <td><h4><span class="label label-danger">You not have enought money!</span></h4></td>
                }
                else { 
                     <td><h4><span class="label label-success">Your financial position is ok. </span></h4></td>
                }
            </tr>
        </table>
      
    </div>
</div>
<div class="panel panel-success">
    
    <div class="panel-body">
        <table>
            <tr>
                <td>
                    @Html.Partial("PieChart")
                </td>
                <td>
                    @Html.Partial("PieChartIncome")
                </td>
            </tr>
        </table>
        </div>
    </div>
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> </div>
    <div class="modal fade" id="myModalCreate" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        ----
    </div>
<div class="modal fade" id="myModalReport" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    ----
</div>
<div class="modal fade" id="myModalDelete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> </div>
    <script type="text/javascript">
        $(function () {

            $('.startPeriod').datepicker();
            $('.endPeriod').datepicker();

            $('body').on("click", "a[data-target=#myModal]", function () {
                var url = $('#myModal').data('url');

                $.get("../MovementOfFunds/Edit/" + $(this).attr('id'), function (data) {
                    $('#myModal').html(data);

                    $('#myModal').modal('show');
                });
            });
            $('body').on("click", "a[data-target=#myModalCreate]", function () {
                var url = $('#myModalCreate').data('url');

                $.get("../MovementOfFunds/Create", function (data) {
                    $('#myModalCreate').html(data);

                    $('#myModalCreate').modal('show');
                });
            });

            $('body').on("click", "a[data-target=#myModalReport]", function () {
                var url = $('#myModalReport').data('url');

                $.get("../MovementOfFunds/PDF/" + $(this).attr('id'), function (data) {
                    //$('#myModalReport').html(data);
                   
                    //$('#myModalReport').modal('show');
                });
            });

            $('body').on("click", "a[data-target=#myModalDelete]", function () {
                var url = $('#myModalDelete').data('url');

                $.get("../MovementOfFunds/Delete/" + $(this).attr('id'), function (data) {
                    //$('#myModalDelete').html(data);
                    //$('#myModalDelete').modal('show');
                    window.location.reload(true);
                });
            });
            $("#myCarousel").hide();
            $("#nav").attr("style", "margin-bottom: 0px !important; position: relative !important;")
        });
    </script>
